package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.io.IOException;

public class LoginPage extends Utils {
    public static String url = "http://112.124.21.155:8080/blog_system/blog_login.html";
    public LoginPage() {
        super(url);
    }

    //检查页面是否加载成功
    public void loginPageRight(){
        driver.findElement(By.cssSelector("body > div.nav > div > div > a:nth-child(1)"));
        driver.findElement(By.cssSelector("body > div.login-container > form"));
    }

    //检查登录功能-成功登录
    public void LoginSuc() throws IOException {
        WebElement username =  driver.findElement(By.cssSelector("#username"));
        WebElement password =  driver.findElement(By.cssSelector("#password"));

        username.clear();
        password.clear();

        username.sendKeys("zhangsan");
        password.sendKeys("123");
        driver.findElement(By.cssSelector("#btn_login_submit")).click();

        WebElement exit = driver.findElement(By.cssSelector("body > div.nav > div > div > a:nth-child(3)"));

        String expect = driver.getTitle();
        assert expect.equals("博客列表");

        getScreenShot(getClass().getName());

        driver.navigate().back();
    }

    //检查登录功能-登陆失败
    public void LoginFail() throws IOException {
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#username")).sendKeys("admin111");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#btn_login_submit")).click();

        //
        String res = driver.findElement(By.cssSelector("body")).getText();


        getScreenShot(getClass().getName());

        assert res.equals("用户名或密码错误!");
    }
}
